package filter;

import utils.CookieUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ManagerFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("ManagerFilter处理");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        Cookie cookie = CookieUtils.findCookie("uid",httpServletRequest.getCookies());

        if (cookie.getValue().length() == 0) {
            servletResponse.getWriter().write("ManagerFilter: you need to login");
        } else {
            System.out.println(cookie.getName() + "访问数据");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
